package net.dgg.zz.production.nodeOverdue.service;

import com.alibaba.fastjson.JSONObject;
import net.dgg.zz.common.dao.OrderDao;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.kafka.vo.AfterSaleChangeStatusMessageVo;
import net.dgg.zz.kafka.vo.AfterSaleChangeStatusProductItemVo;
import net.dgg.zz.kafka.vo.AfterSaleProductHandleItemVo;
import net.dgg.zz.kafka.vo.AfterSaleProductHandleMessageVo;
import net.dgg.zz.production.entity.ProductOrder;
import net.dgg.zz.production.entity.UpdateZzscOrderDto;
import net.dgg.zz.production.excption.ProductOrderExcption;
import net.dgg.zz.production.inProcess.dao.ProductOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ProductOrderService {
    @Autowired
    private ProductOrderDao productOrderDao;
    @Autowired
    private OrderDao orderDao;

    public List<ProductOrder> getProductOrder(Map param){
        return productOrderDao.selectByOrderNo(param);
    }
    public ProductOrder getProductOrderById(Long id){
        return productOrderDao.selectByPrimaryKey(id);
    }
    /**
     * 订单中心修改签单人信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void executeUpdateSignUser(JSONObject jsonObject) {
        UpdateZzscOrderDto updateZzscOrder = new UpdateZzscOrderDto();
        updateZzscOrder.setOrderId(jsonObject.getLong("orderId"));
        updateZzscOrder.setSignUserId(jsonObject.getLong("signUserId"));
        updateZzscOrder.setSignUserName(jsonObject.getString("signUserName"));
        updateZzscOrder.setSignUserOrgId(jsonObject.getLong("signUserOrgId"));
        updateZzscOrder.setSignUserOrgName(jsonObject.getString("signUserOrgName"));
        updateZzscOrder.setSignOrgId(jsonObject.getLong("signOrgId"));
        updateZzscOrder.setSignOrgName(jsonObject.getString("signOrgName"));
        //修改order
        orderDao.updateOrderInfo(updateZzscOrder);
        //修改product_order
        productOrderDao.updateOrderInfo(updateZzscOrder);
    }

	public void afterSaleStatus(AfterSaleChangeStatusMessageVo vo) {
        if(StringUtils.isEmpty(vo)){
            throw new ProductOrderExcption("参数为空！");
        }
        List<AfterSaleChangeStatusProductItemVo> productList = vo.getProductList();
        for(AfterSaleChangeStatusProductItemVo itemVo : productList){
            if(StringUtils.isEmpty(itemVo.getScOrderId())){
                continue;
            }
            ProductOrder productOrder = productOrderDao.getProductOrderByScOrderId(itemVo.getScOrderId());
            if(StringUtils.isEmpty(productOrder)){
                continue;
            }
            ProductOrder updateProductOrder = new ProductOrder();
            updateProductOrder.setId(productOrder.getId());
            updateProductOrder.setAfterSaleNo(vo.getAfterSaleNo());
            updateProductOrder.setAfterSaleStatus(vo.getStatus());
            updateProductOrder.setUpdateTime(new Date());
            updateProductOrder.setUpdaterName("售后状态修改");
            productOrderDao.updateAfterSaleStatus(updateProductOrder);
        }
	}

    public void afterSaleProHandle(AfterSaleProductHandleMessageVo vo) {
        if(StringUtils.isEmpty(vo)){
            throw new ProductOrderExcption("参数为空！");
        }
        List<AfterSaleProductHandleItemVo> productList = vo.getProductHandleList();
        for(AfterSaleProductHandleItemVo itemVo : productList){
            if(org.apache.commons.lang3.StringUtils.isEmpty(itemVo.getScOrderId())){
                continue;
            }
            ProductOrder productOrder = productOrderDao.getProductOrderByScOrderId(itemVo.getScOrderId());
            if(StringUtils.isEmpty(productOrder)){
                continue;
            }
            ProductOrder updateProductOrder = new ProductOrder();
            updateProductOrder.setId(productOrder.getId());
            updateProductOrder.setProductHandleResultType(itemVo.getProductHandleResultType());
            updateProductOrder.setUpdateTime(new Date());
            updateProductOrder.setUpdaterName("售后处理结果修改");
            productOrderDao.updateAfterSaleStatus(updateProductOrder);
        }
    }

    public void updateCustomerName(Map param) {
        if(param.isEmpty()){
            throw new ProductOrderExcption("参数为空！");
        }
        String customerId = param.get("customerId").toString();
        String customerName = param.get("customerName").toString();
        if(net.dgg.iboss.base.util.StringUtils.isEmpty(customerId)){
            throw new ProductOrderExcption("客户ID为空！");
        }
        if(net.dgg.iboss.base.util.StringUtils.isEmpty(customerName)){
            throw new ProductOrderExcption("客户名称为空！");
        }
        productOrderDao.updateCustomerName(customerId,customerName);
    }
}
